/**
 * Created by yoya on 2016/11/3.
 */
(function () {
    //获取url中指定参数
        //页面加载完成
        document.onreadystatechange = completeLoading;
        function completeLoading() {
            if (document.readyState == "complete") {
                o_ajax({
                    type: "get",
                    url: "/data-json/i.json",
                    data: {
                        t: new Date().getTime(),
                        u: document.referrer || "_"
                    }
                })
            }
        }

        var interval_output = setInterval(function () {
             sendOnlinemsg()
         }, 5000);

    //发送信息
        function sendOnlinemsg() {
            o_ajax({
                url: "/data-json/s.json",
                type: "get",
                data: {
                    t: new Date().getTime()
                }
            })
        }
    //ajax请求
    function o_ajax(obj) {
        var setting = {
            type : "get",
            url : "#",
            dataType : "text",
            async : "true",
            data : {}
        }
        //覆盖初始化
        for(var key in obj){
            setting[key] = obj[key]
        }
        var xhr = null ;
        if(window.XMLHttpRequest){
            xhr = new XMLHttpRequest();
        }else {
            xhr = new ActiveXObject("Microsoft.XMLHTTP") //ie
        }
        //拼接字符串
        var str = "";
        for(var attr in setting.data){
            str += attr + "=" + setting.data[attr] + "&";
        }
        if (str.length > 0){
            str = str.substring(0 , str.length - 1);
        }
        //处理get请求
        if(setting.type == "get"){
            setting.url = setting.url + "?" + str
        }
        //发送
        xhr.open(setting.type,setting.url,setting.async)

        //处理post请求
        var param = null;
        if(setting.type == "post"){
            param = str
            xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded")
        }
        xhr.send(param)
    }

})()